/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.gapvis;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComponent;
import javax.swing.TransferHandler;

import at.tugraz.kmi.css.analyzer.UserCompetence;
import edu.uci.ics.jung.visualization.VisualizationViewer;

/**
 * A Drag and Drop Handler
 * 
 * @author Georg Öttl
 * 
 */
public class CompetenceDropTransferHandler extends TransferHandler {

	private static final long						serialVersionUID	= 1L;
	private final CompetenceProficiencyEditorVis	competenceVis;
	private final VisualizationViewer<?, ?>			jpanelVis;

	Logger											log					= Logger.getLogger(CompetenceDropTransferHandler.class
																				.getName());

	public CompetenceDropTransferHandler(VisualizationViewer<?, ?> jpanelVis,
			CompetenceProficiencyEditorVis competenceVis) {
		this.competenceVis = competenceVis;
		this.jpanelVis = jpanelVis;
	}

	@Override
	public boolean canImport(TransferHandler.TransferSupport info) {
		return true;
	}

	@Override
	public boolean importData(TransferHandler.TransferSupport info) {
		if (!info.isDrop()) {
			return false;
		}
		DataFlavor flavor = new DataFlavor(UserCompetence[].class, "JobProfile");
		// Get the string that is being dropped.
		Transferable t = info.getTransferable();
		try {
			Object[] transferData = (Object[]) t.getTransferData(flavor);
			DropLocation location = info.getDropLocation();
			for (Object element : transferData) {
				UserCompetence object = (UserCompetence) element;
				Point dropPoint = location.getDropPoint();
				// TODO make competences present radial or so when dropped
				// to the pane
				competenceVis.addCompetence(object, dropPoint);
			}

			jpanelVis.repaint();
			return true;
		} catch (Exception e) {
			log.log(Level.FINE, "error on import data transfer dnd handler.", e);
			return false;
		}

	}

	@Override
	public int getSourceActions(JComponent c) {
		return COPY;
	}

}
